<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>22.4. Zend_Layout Advanced Usage</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="prev" href="zend.layout.options.html" title="22.3. Zend_Layout Configuration Options">
<link rel="next" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.layout.advanced.html#zend.layout.advanced.view" title="22.4.1. Custom View Objects">
<link rel="subsection" href="zend.layout.advanced.html#zend.layout.advanced.plugin" title="22.4.2. Custom Front Controller Plugins">
<link rel="subsection" href="zend.layout.advanced.html#zend.layout.advanced.helper" title="22.4.3. Custom Action Helpers">
<link rel="subsection" href="zend.layout.advanced.html#zend.layout.advanced.inflector" title="22.4.4. Custom Layout Script Path Resolution: Using the Inflector">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">22.4. Zend_Layout Advanced Usage</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.layout.options.html">Prev</a> </td>
<th width="60%" align="center">Chapter 22. Zend_Layout</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.ldap.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.layout.advanced"></a>22.4. Zend_Layout Advanced Usage</h2></div></div></div>
<p>
        <code class="code">Zend_Layout</code> has a number of use cases for the advanced
        developer who wishes to adapt it for different view implementations,
        file system layouts, and more.
    </p>
<p>
        The major points of extension are:
    </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                <span class="emphasis"><em>Custom view objects.</em></span>
                <code class="code">Zend_Layout</code> allows you to utilize any class that
                implements <code class="code">Zend_View_Interface</code>. 
        </p></li>
<li><p>
                <span class="emphasis"><em>Custom front controller plugins.</em></span>
                <code class="code">Zend_Layout</code> ships with a standard front controller
                plugin that automates rendering of layouts prior to returning
                the response. You can substitute your own plugin.
        </p></li>
<li><p>
                <span class="emphasis"><em>Custom action helpers.</em></span>
                <code class="code">Zend_Layout</code> ships with a standard action helper
                that should be suitable for most needs as it is a dumb proxy
                to the layout object itself.
        </p></li>
<li><p>
                <span class="emphasis"><em>Custom layout script path resolution</em></span>.
                <code class="code">Zend_Layout</code> allows you to use your own <a href="zend.filter.inflector.html" title="16.6. Zend_Filter_Inflector">inflector</a> for layout
                script path resolution, or simply to modify the attached
                inflector to specify your own inflection rules.
        </p></li>
</ul></div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.advanced.view"></a>22.4.1. Custom View Objects</h3></div></div></div>
<p>
            <code class="code">Zend_Layout</code> allows you to use any class implementing
            <code class="code">Zend_View_Interface</code> or extending
            <code class="code">Zend_View_Abstract</code> for rendering your layout script.
            Simply pass in your custom view object as a parameter to the
            constructor/<code class="code">startMvc()</code>, or set it using the
            <code class="code">setView()</code> accessor:
        </p>
<pre class="programlisting">&lt;?php
$view = new My_Custom_View();
$layout-&gt;setView($view);
?&gt;</pre>
<div class="note"><table border="0" summary="Note: Not all Zend_View implementations are equal">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Not all Zend_View implementations are equal</th>
</tr>
<tr><td align="left" valign="top">
<p>
                While <code class="code">Zend_Layout</code> allows you to use any class
                implementing <code class="code">Zend_View_Interface</code>, you may run into
                issues if they can not utilize the various
                <code class="code">Zend_View</code> helpers, particularly the layout and
                <a href="zend.view.helpers.html#zend.view.helpers.initial.placeholder" title="48.4.1.3. Placeholder Helper">placeholder</a>
                helpers. This is because <code class="code">Zend_Layout</code> makes
                variables set in the object available via itself and
                <a href="zend.view.helpers.html#zend.view.helpers.initial.placeholder" title="48.4.1.3. Placeholder Helper">placeholders</a>. 
            </p>
<p>
                If you need to use a custom <code class="code">Zend_View</code>
                implementation that does not support these helpers, you will
                need to find a way to get the layout variables to the view. This
                can be done by either extending the <code class="code">Zend_Layout</code>
                object and altering the <code class="code">render()</code> method to pass
                variables to the view, or creating your own plugin class that
                passes them prior to rendering the layout.
            </p>
<p>
                Alternately, if your view implementation supports any sort of
                plugin capability, you can access the variables via the
                'Zend_Layout' placeholder, using the <a href="zend.view.helpers.html#zend.view.helpers.initial.placeholder" title="48.4.1.3. Placeholder Helper">placeholder
                    helper</a>:
            </p>
<pre class="programlisting">&lt;?php
$placeholders = new Zend_View_Helper_Placeholder();
$layoutVars   = $placeholders-&gt;placeholder('Zend_Layout')-&gt;getArrayCopy();
?&gt;</pre>
</td></tr>
</table></div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.advanced.plugin"></a>22.4.2. Custom Front Controller Plugins</h3></div></div></div>
<p>
            When used with the MVC components, <code class="code">Zend_Layout</code>
            registers a front controller plugin that renders the layout as the
            last action prior to exiting the dispatch loop. In most cases, the
            default plugin will be suitable, but should you desire to write
            your own, you can specify the name of the plugin class to load by
            passing the <code class="code">pluginClass</code> option to the
            <code class="code">startMvc()</code> method.
        </p>
<p>
            Any plugin class you write for this purpose will need to extend
            <code class="code">Zend_Controller_Plugin_Abstract</code>, and should accept a
            layout object instance as an argument to the constructor. Otherwise,
            the details of your implementation are up to you.
        </p>
<p>
            The default plugin class used is
            <code class="code">Zend_Layout_Controller_Plugin_Layout</code>.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.advanced.helper"></a>22.4.3. Custom Action Helpers</h3></div></div></div>
<p>
            When used with the MVC components, <code class="code">Zend_Layout</code>
            registers an action controller helper with the helper broker. The
            default helper,
            <code class="code">Zend_Layout_Controller_Action_Helper_Layout</code>, acts as a
            dumb proxy to the layout object instance itself, and should be
            suitable for most use cases.
        </p>
<p>
            Should you feel the need to write custom functionality, simply write
            an action helper class extending
            <code class="code">Zend_Controller_Action_Helper_Abstract</code> and pass the
            class name as the <code class="code">helperClass</code> option to the
            <code class="code">startMvc()</code> method.  Details of the implementation are
            up to you.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.advanced.inflector"></a>22.4.4. Custom Layout Script Path Resolution: Using the Inflector</h3></div></div></div>
<p>
            <code class="code">Zend_Layout</code> uses <code class="code">Zend_Filter_Inflector</code> to
            establish a filter chain for translating a layout name to a layout
            script path. By default, it uses the rules 'CamelCaseToDash'
            followed by 'StringToLower', and the suffix 'phtml' to transform the
            name to a path. As some examples:
        </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                    'foo' will be transformed to 'foo.phtml'.
            </p></li>
<li><p>
                    'FooBarBaz' will be transformed to 'foo-bar-baz.phtml'.
            </p></li>
</ul></div>
<p>
            You have three options for modifying inflection: modify the
            inflection target and/or view suffix via <code class="code">Zend_Layout</code>
            accessors, modify the inflector rules and target of the inflector
            associated with the <code class="code">Zend_Layout</code> instance, or create
            your own inflector instance and pass it to
            <code class="code">Zend_Layout::setInflector()</code>.
        </p>
<div class="example">
<a name="zend.layout.advanced.inflector.accessors"></a><p class="title"><b>Example 22.4. Using Zend_Layout accessors to modify the inflector</b></p>
<div class="example-contents">
<p>
                The default <code class="code">Zend_Layout</code> inflector uses static
                references for the target and view script suffix, and 
                has accessors for setting these values.
            </p>
<pre class="programlisting">&lt;?php
// Set the inflector target:
$layout-&gt;setInflectorTarget('layouts/:script.:suffix');

// Set the layout view script suffix:
$layout-&gt;setViewSuffix('php');
?&gt;</pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="zend.layout.advanced.inflector.directmodification"></a><p class="title"><b>Example 22.5. Direct modification of Zend_Layout inflector</b></p>
<div class="example-contents">
<p>
                Inflectors have a target and one or more rules. The default
                target used with <code class="code">Zend_Layout</code> is ':script.:suffix';
                ':script' is passed the registered layout name, while ':suffix'
                is a static rule of the inflector.
            </p>
<p>
                Let's say you want the layout script to end in the suffix
                'html', and that you want to separate MixedCase and camelCased words with
                underscores instead of dashes, and not lowercase the name.
                Additionally, you want it to look in a 'layouts' subdirectory
                for the script.
            </p>
<pre class="programlisting">&lt;?php
$layout-&gt;getInflector()-&gt;setTarget('layouts/:script.:suffix')
                       -&gt;setStaticRule('suffix', 'html')
                       -&gt;setFilterRule(array('CamelCaseToUnderscore'));
?&gt;</pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="zend.layout.advanced.inflector.custom"></a><p class="title"><b>Example 22.6. Custom inflectors</b></p>
<div class="example-contents">
<p>
                In most cases, modifying the existing inflector will be enough.
                However, you may have an inflector you wish to use in several
                places, with different objects of different types.
                <code class="code">Zend_Layout</code> supports this.
            </p>
<pre class="programlisting">&lt;?php
$inflector = new Zend_Filter_Inflector('layouts/:script.:suffix');
$inflector-&gt;addRules(array(
    ':script' =&gt; array('CamelCaseToUnderscore'),
    'suffix'  =&gt; 'html'
));
$layout-&gt;setInflector($inflector);
?&gt;</pre>
</div>
</div>
<br class="example-break"><div class="note"><table border="0" summary="Note: Inflection can be disabled">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Inflection can be disabled</th>
</tr>
<tr><td align="left" valign="top"><p>
                Inflection can be disabled and enabled using accessors on the
                <code class="code">Zend_Layout</code> object. This can be useful if you want
                to specify an absolute path for a layout view script, or know
                that the mechanism you will be using for specifying the layout
                script does not need inflection. Simply use the
                <code class="code">enableInflection()</code> and
                <code class="code">disableInflection()</code> methods.
            </p></td></tr>
</table></div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.layout.options.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.layout.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.ldap.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">22.3. Zend_Layout Configuration Options </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Chapter 23. Zend_Ldap</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
